<template>
	<div>
		<jk-table
			ref="frontGroupSalaryMonthReportTable"
			border
			show-footer
			:span-method="mergeColMethod"
			:footer-method="footerMethod"
			:data="tableData"
			:height="height"
			:loading="loading"
		>
			<vxe-column
				v-for="(item, index) in tableColumnList"
				:key="index"
				:title="item.title"
				:align="item.align"
				:field="item.keyName"
				:sortable="item.sortable"
				:fixed="item.fixed ? item.fixed : ''"
				:min-width="item.minWidth"
				:width="item.width"
			>
				<template slot-scope="scope">
					<span v-if="item.keyName === 'deviceRecordNumber'" @click="onMachine(scope.row)">{{ scope.row.deviceRecordNumber }}</span>
					<span v-else-if="item.keyName === 'machineEfficiency'">{{ scope.row.machineEfficiency !== null ? scope.row.machineEfficiency + '%' : '' }}</span>
					<span v-else>{{ scope.row[item.keyName] }}</span>
				</template>
			</vxe-column>
			<!--			<vxe-column title="操作" align="center" fixed="right" :width="180">-->
			<!--				<template slot-scope="scope">-->
			<!--					<div class="flex-center">-->
			<!--						<jk-button class="item-gap" icon="el-icon-edit" size="mini" type="success" @click="onEditBtn(scope.row.id)">编辑</jk-button>-->
			<!--						<el-dropdown>-->
			<!--							<jk-button type="primary" size="mini">-->
			<!--								更多<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
			<!--							</jk-button>-->
			<!--							<el-dropdown-menu slot="dropdown">-->
			<!--								<el-dropdown-item>-->
			<!--									<div @click="particulars(scope.row.id)">详情</div>-->
			<!--								</el-dropdown-item>-->
			<!--								<el-dropdown-item disabled>-->
			<!--									<div>删除</div>-->
			<!--								</el-dropdown-item>-->
			<!--							</el-dropdown-menu>-->
			<!--						</el-dropdown>-->
			<!--					</div>-->
			<!--				</template>-->
			<!--			</vxe-column>-->
		</jk-table>
		<detail-dialog
			:search-params="searchParams"
			:data-id="dataId"
			:page-type="pageType"
			:data-item="selectedRowItem"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
		></detail-dialog>
	</div>
</template>

<script>
    import detailDialog from './detail-dialog';
    import { systemUserColumnConfigGet } from '../../../../api/system/column-config';
    import XEUtils from 'xe-utils';
    export default {
        components: { detailDialog },
        props: {
            tableData: {
                type: Array,
                default() {
                    return [];
                }
            },
            searchParams: {
                type: Object,
                default() {
                    return {};
                }
            },
            height: {
                type: [String, Number],
                default: 0
            },
            loading: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                tableColumnList: [],
                selectedRowItem: {},
                saveDialogState: false,
                noCalKey: ['outputM', 'outputKg', 'energy'],
                pageType: '',
                dataId: null
            };
        },
        methods: {
            mergeColMethod({ row, _rowIndex, column, visibleData }) {
                const prevRow = visibleData[_rowIndex - 1];
                let nextRow = visibleData[_rowIndex + 1];
                if (column.property === 'belongDate') {
                    if (prevRow && prevRow.belongDate === row.belongDate) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.belongDate === row.belongDate) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                }
            },
            footerMethod({ columns, data }) {
                return [
                    columns.map((column, columnIndex) => {
                        if (columnIndex === 0) {
                            // return `合计: ${this.totalData.totalOutputM || 0}M; ${this.totalData.totalOutputKg || 0}KG`;
                            return `合计`;
                        }
                        if (this.noCalKey.includes(column.property)) {
                            return XEUtils.sum(data, column.property);
                        }
                        return null;
                    })
                ];
            },
            // 详情
            particulars(id) {
                this.pageType = 'detail';
                this.dataId = id;
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(id) {
                this.pageType = 'edit';
                this.dataId = id;
                this.saveDialogState = true;
            },
            getMenuColumnListRequest() {
                return systemUserColumnConfigGet({ path: 'ScheduleShiftProductionReportRecord', filterShow: true }).then(res => {
                    if (res.data.status === 200) {
                        this.tableColumnList = res.data.res.columnConfigList && res.data.res.columnConfigList.length ? res.data.res.columnConfigList[0].itemList : [];
                    }
                });
            },
            onVisibleChange(e) {
                this.saveDialogState = e;
                this.$emit('getListRequest');
            },
            onMachine(rowItem) {
                this.selectedRowItem = rowItem;
                if (rowItem.deviceRecordNumber.includes('LT') || rowItem.deviceRecordNumber.includes('lt')) {
                    // this.detailDialogState = true;
                }
            }
        },
        mounted() {
            this.getMenuColumnListRequest();
        }
    };
</script>
